#include "MpiException.h"

#include "mpi.h"

using namespace SG::Algebra;

namespace // Anonymous
{
    std::string error_string (int err)
    {
        char buffer[MPI_MAX_ERROR_STRING];
        int  len;
        int  status = MPI_Error_string (err, buffer, &len);
        if (status == MPI_SUCCESS)
        {
            return std::string (buffer);
        }
        else
        {
            std::ostringstream out;
            if (status == MPI_ERR_ARG)
            {
                out << "<invalid MPI error code " << err << ">";
            }
            else
            {
                out << "<got error " << status << " while probing MPI error " << err << ">";
            }
            return out.str ();
        }
    }
}

//=============================================================================
// MpiCommunicator

MpiException::MpiException (const char* routine, int result_code)
{
    message.append (routine);
    message.append (": ");
    message.append (error_string (result_code));
}

MpiException::~MpiException ()
{}
